<?php

class App_Model_DbTable_User_Account_Base_Username extends Ext_Db_Table  
{  
	/** Table name */  
    protected $_name = 'user_account_base_username';
    
    private static $_userAccountBaseUsernameRow = array();
    
	public function getRowByUserId($_userId)
	{
		if(!$_userId)
		{
			return "";
		}
		
		if(isset(self::$_userAccountBaseUsernameRow['userId'][$_userId]))
		{
			return self::$_userAccountBaseUsernameRow['userId'][$_userId];
		}
		
		$db = $this->getAdapter();
		$safe = $db->quoteInto(" userId = ? ", $_userId);
		$row = $this->fetchRow($safe);
		if (!$row) {
			return "";
		}
		$_userAccountBaseUsernameRow = $row->toArray();

		self::$_userAccountBaseUsernameRow['userId'][$_userId] = $_userAccountBaseUsernameRow;
		$_username = $_userAccountBaseUsernameRow['username'];
		if($_username)
		{
			self::$_userAccountBaseUsernameRow['username'][$_username] = $_userAccountBaseUsernameRow;
		}
		
		return $_userAccountBaseUsernameRow;
	}
	
	public function getRowByUsername($_username)
	{
		if(!$_username)
		{
			return "";
		}
		
		if(isset(self::$_userAccountBaseUsernameRow['username'][$_username]))
		{
			return self::$_userAccountBaseUsernameRow['username'][$_username];
		}
		
		$db = $this->getAdapter();
		$safe = $db->quoteInto(" username = ? ", $_username);
		$row = $this->fetchRow($safe);
		if (!$row) {
			return "";
		}
		$_userAccountBaseUsernameRow = $row->toArray();

		self::$_userAccountBaseUsernameRow['username'][$_username] = $_userAccountBaseUsernameRow;
		$_userId = $_userAccountBaseUsernameRow['userId'];
		if($_userId)
		{
			self::$_userAccountBaseUsernameRow['userId'][$_userId] = $_userAccountBaseUsernameRow;
		}
				
		return $_userAccountBaseUsernameRow;
	}
	
	public function addNew($inputArray)
	{
		$sql_data_array = array();
		if(array_key_exists('userId', $inputArray)) $sql_data_array['userId'] = $inputArray['userId'];
		if(array_key_exists('username', $inputArray)) $sql_data_array['username'] = $inputArray['username'];
		if(!$sql_data_array)
		{
			return "";
		}
		
		try {
			$rows_affected = $this->insert($sql_data_array);
			$last_insert_id = $this->getAdapter()->lastInsertId();
		}catch (Exception $e){
			trigger_error($e->getMessage(), E_USER_WARNING);
		}
		return $last_insert_id;
	}
	
	public function addOld($inputArray, $userId)
	{
		$sql_data_array = array();
		if(array_key_exists('username', $inputArray)) $sql_data_array['username'] = $inputArray['username'];
		if(!$sql_data_array)
		{
			return "";
		}
		
		try {
			$db = $this->getAdapter();
			$where = $db->quoteInto(" userId = ? ", $userId);
			$rows_affected = $this->update($sql_data_array, $where);
		}catch (Exception $e){
			trigger_error($e->getMessage(), E_USER_WARNING);
		}
		
		return "";
	}
	
}  

